home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-aux / book.aux / fog.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  5KB  |  180 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  fog.c
  39.  *  This program draws 5 red teapots, each at a different 
  40.  *  z distance from the eye, in different types of fog.  
  41.  *  Pressing the left mouse button chooses between 3 types of 
  42.  *  fog:  exponential, exponential squared, and linear.  
  43.  *  In this program, there is a fixed density value, as well 
  44.  *  as fixed start and end values for the linear fog.
  45.  */
  46. #include <GL/gl.h>
  47. #include <GL/glu.h>
  48. #include <math.h>
  49. #include <stdio.h>
  50. #include <GL/glaux.h>
  51.  
  52. GLint fogMode;
  53.  
  54. void cycleFog(AUX_EVENTREC * event)
  55. {
  56.   if (fogMode == GL_EXP) {
  57.     fogMode = GL_EXP2;
  58.     printf("Fog mode is GL_EXP2\n");
  59.   }
  60.   else if (fogMode == GL_EXP2) {
  61.     fogMode = GL_LINEAR;
  62.     printf("Fog mode is GL_LINEAR\n");
  63.     glFogf(GL_FOG_START, 1.0);
  64.     glFogf(GL_FOG_END, 5.0);
  65.   }
  66.   else if (fogMode == GL_LINEAR) {
  67.     fogMode = GL_EXP;
  68.     printf("Fog mode is GL_EXP\n");
  69.   }
  70.   glFogi(GL_FOG_MODE, fogMode);
  71. }
  72.  
  73. /*
  74.  * Initialize z-buffer, projection matrix, light source, 
  75.  * *  and lighting model.  Do not specify a material property here.
  76.  */
  77. void myinit(void)
  78. {
  79.   GLfloat position[] =
  80.   {0.0, 3.0, 3.0, 0.0};
  81.   GLfloat local_view[] =
  82.   {0.0};
  83.  
  84.   glEnable(GL_DEPTH_TEST);
  85.   glDepthFunc(GL_LESS);
  86.  
  87.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  88.   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  89.  
  90.   glFrontFace(GL_CW);
  91.   glEnable(GL_LIGHTING);
  92.   glEnable(GL_LIGHT0);
  93.   glEnable(GL_AUTO_NORMAL);
  94.   glEnable(GL_NORMALIZE);
  95.   glEnable(GL_FOG);
  96.   {
  97.     GLfloat fogColor[4] =
  98.     {0.5, 0.5, 0.5, 1.0};
  99.  
  100.     fogMode = GL_EXP;
  101.     glFogi(GL_FOG_MODE, fogMode);
  102.     glFogfv(GL_FOG_COLOR, fogColor);
  103.     glFogf(GL_FOG_DENSITY, 0.35);
  104.     glHint(GL_FOG_HINT, GL_DONT_CARE);
  105.     glClearColor(0.5, 0.5, 0.5, 1.0);
  106.   }
  107. }
  108.  
  109. void renderRedTeapot(GLfloat x, GLfloat y, GLfloat z)
  110. {
  111.   float mat[4];
  112.  
  113.   glPushMatrix();
  114.   glTranslatef(x, y, z);
  115.   mat[0] = 0.1745;
  116.   mat[1] = 0.01175;
  117.   mat[2] = 0.01175;
  118.   mat[3] = 1.0;
  119.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
  120.   mat[0] = 0.61424;
  121.   mat[1] = 0.04136;
  122.   mat[2] = 0.04136;
  123.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
  124.   mat[0] = 0.727811;
  125.   mat[1] = 0.626959;
  126.   mat[2] = 0.626959;
  127.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
  128.   glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0);
  129.   glColor3f(1, 1, 1);
  130.   auxSolidTeapot(1.0);
  131.   glPopMatrix();
  132. }
  133.  
  134. /*
  135.  * display() draws 5 teapots at different z positions.
  136.  */
  137. void display(void)
  138. {
  139.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  140.   renderRedTeapot(-4.0, -0.5, -1.0);
  141.   renderRedTeapot(-2.0, -0.5, -2.0);
  142.   renderRedTeapot(0.0, -0.5, -3.0);
  143.   renderRedTeapot(2.0, -0.5, -4.0);
  144.   renderRedTeapot(4.0, -0.5, -5.0);
  145.   glFlush();
  146. }
  147.  
  148. void myReshape(int w, int h)
  149. {
  150.   glViewport(0, 0, w, h);
  151.   glMatrixMode(GL_PROJECTION);
  152.   glLoadIdentity();
  153.   if (w <= (h * 3))
  154.     glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w,
  155.         2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0);
  156.   else
  157.     glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3),
  158.         6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0);
  159.   glMatrixMode(GL_MODELVIEW);
  160.   glLoadIdentity();
  161. }
  162.  
  163. /*
  164.  * Main Loop
  165.  * *  Open window with initial window size, title bar, 
  166.  * *  RGBA display mode, depth buffer, and handle input events.
  167.  */
  168. int main(int argc, char **argv)
  169. {
  170.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  171.   auxInitPosition(0, 0, 450, 150);
  172.   if (!auxInitWindow(argv[0]))
  173.     auxQuit();
  174.   auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, cycleFog);
  175.   myinit();
  176.   auxReshapeFunc(myReshape);
  177.   auxMainLoop(display);
  178.   return 0;
  179. }
  180.